package com.ruicar.afs.cloud.manage.detail.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import com.ruicar.afs.cloud.manage.common.entity.ManageApplyRecord;
import com.ruicar.afs.cloud.manage.common.entity.ManageApprovalRecord;
import com.ruicar.afs.cloud.manage.common.service.ManageApplyRecordService;
import com.ruicar.afs.cloud.manage.common.service.ManageApprovalRecordService;
import com.ruicar.afs.cloud.manage.detail.vo.ManageApplyRecordVo;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 合同明细信息查询
 */
@Slf4j
@RestController
@RequestMapping("/contractDetailManage")
@AllArgsConstructor
public class ContractDetailController {

    private final ManageApplyRecordService manageApplyRecordService;
    private final ManageApprovalRecordService manageApprovalRecordService;

    @ApiOperation(value = "根据合同号码查询合同变更记录列表")
    @PostMapping("/detail")
    public IResponse<List<ManageApplyRecordVo>> contractDetail(@RequestParam("contractNo") String contractNo){
        List<ManageApplyRecordVo> applyRecordVoList = new ArrayList<>();
        List<ManageApplyRecord> manageApplyRecords = manageApplyRecordService.list(Wrappers.<ManageApplyRecord>lambdaQuery().eq(ManageApplyRecord::getContractNo, contractNo));
        if (EmptyUtils.isNotEmpty(manageApplyRecords)){
            manageApplyRecords.forEach(manageApplyRecord -> {
                ManageApplyRecordVo applyRecordVo = new ManageApplyRecordVo();
                applyRecordVo.setContractNo(manageApplyRecord.getContractNo());
                applyRecordVo.setApplyTime(EmptyUtils.isNotEmpty(manageApplyRecord.getApplyTime())?DateUtil.format(manageApplyRecord.getApplyTime(),"yyyy/MM/dd HH:mm:ss"):"");
                applyRecordVo.setEndDate(EmptyUtils.isNotEmpty(manageApplyRecord.getEndDate())?DateUtil.format(manageApplyRecord.getEndDate(),"yyyy/MM/dd HH:mm:ss"):"");
                applyRecordVo.setBusinessSource(manageApplyRecord.getBusinessSource());
                applyRecordVo.setCaseNo(manageApplyRecord.getCaseNo());
                applyRecordVo.setCaseStatus(manageApplyRecord.getCaseStatus());
                applyRecordVo.setContractBusiness(manageApplyRecord.getContractBusiness());
                applyRecordVo.setDataSource(manageApplyRecord.getDataSource());
                applyRecordVo.setCreateBy(manageApplyRecord.getCreateBy());
                applyRecordVo.setId(manageApplyRecord.getId());
                applyRecordVoList.add(applyRecordVo);
            });
            return IResponse.success(applyRecordVoList);
        }else{
            return IResponse.fail("非法的合同号");
        }

    }
    @ApiOperation(value = "合同明细查询查看日志")
    @PostMapping("/lookLog")
    public IResponse<List<ManageApplyRecordVo>> lookLog(@RequestParam("contractNo") String contractNo){
        List<ManageApplyRecordVo> applyRecordVoList = new ArrayList<>();
        List<ManageApplyRecord> manageApplyRecords = manageApplyRecordService.list(Wrappers.<ManageApplyRecord>lambdaQuery().eq(ManageApplyRecord::getContractNo, contractNo));
        if (EmptyUtils.isNotEmpty(manageApplyRecords)){
            manageApplyRecords.forEach(manageApplyRecord -> {
                ManageApplyRecordVo applyRecordVo = new ManageApplyRecordVo();
                applyRecordVo.setContractNo(manageApplyRecord.getContractNo());
                applyRecordVo.setApplyTime(EmptyUtils.isNotEmpty(manageApplyRecord.getApplyTime())?DateUtil.format(manageApplyRecord.getApplyTime(),"yyyy/MM/dd HH:mm:ss"):"");
                applyRecordVo.setEndDate(EmptyUtils.isNotEmpty(manageApplyRecord.getEndDate())?DateUtil.format(manageApplyRecord.getEndDate(),"yyyy/MM/dd HH:mm:ss"):"");
                applyRecordVo.setBusinessSource(manageApplyRecord.getBusinessSource());
                applyRecordVo.setCaseNo(manageApplyRecord.getCaseNo());
                applyRecordVo.setCaseStatus(manageApplyRecord.getCaseStatus());
                applyRecordVo.setContractBusiness(manageApplyRecord.getContractBusiness());
                applyRecordVo.setDataSource(manageApplyRecord.getDataSource());
                applyRecordVo.setCreateBy(manageApplyRecord.getCreateBy());
                applyRecordVo.setId(manageApplyRecord.getId());
                applyRecordVoList.add(applyRecordVo);
            });
            return IResponse.success(applyRecordVoList);
        }else{
            return IResponse.success("此合同号暂无日志");
        }

    }
    @ApiOperation(value = "根据id查询合同审批记录列表")
    @PostMapping("/recordDetail")
    public IResponse<ManageApprovalRecord> recordDetail(@RequestParam("bizDataId") String bizDataId){
        List<ManageApprovalRecord> manageApprovalRecords = manageApprovalRecordService.list(Wrappers.<ManageApprovalRecord>lambdaQuery().eq(ManageApprovalRecord::getBizDataId, bizDataId));
        return IResponse.success(manageApprovalRecords);
    }
}
